<hr/>
<div class="row-fluid">
    <div class="span12">
        <#if sale.status != "confirmed">
            <button class="btn btn-success" id="btn_addProduct">添加商品</button>
            <#if sale.saleId??>
                <button class="btn btn-success" id="btn_confirm">保存订单</button>
            </#if>
        </#if>
        <button class="btn btn-info">刷新</button>
        <a href="${request.contextPath}/sale/order/list" class="btn btn-info" id="btn_back">返回列表</a>
        <div class="widget-box">
            <div class="widget-title"> <span class="icon"> <i class="icon-pencil"></i> </span>
                <h5>销售订单</h5>
            </div>
            <div class="widget-content nopadding">
                <form class="form-horizontal" method="post" action="${request.contextPath}/sale/order/saveSale" name="sale_form" id="sale_form">
                    <input type="hidden" name="saleId" value="${sale.saleId}" />
                    <div class="control-group">
                        <label class="control-label">会员信息：</label>
                        <div class="controls">
                            <select name="memberId">
                                <#list memberList as member>
                                    <option value="${member.memberId}" <#if member.memberId == sale.member.memberId>selected</#if> >[${member.memberNo}]${member.memberName}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="widget-box">
            <div class="widget-title">
                <span class="icon"><i class="icon-table"></i></span>
                <h5>订单商品</h5>
            </div>
            <div class="widget-content nopadding">
                <table class="table table-bordered table-striped data-table">
                    <thead>
                    <tr>
                        <th width="5%">序号</th>
                        <th width="30%">商品名称</th>
                        <th width="30%">商品规格</th>
                        <th width="10%">数量</th>
                        <th width="10%">价格</th>
                        <#if sale.status != "confirmed">
                            <th width="15%">操作</th>
                        </#if>
                    </tr>
                    </thead>
                    <tbody>
                    <#list sale.detailList as detail>
                        <tr id="tr_${detail.saleDetailId}">
                            <td>${detail_index + 1}</td>
                            <td>${detail.product.productName}</td>
                            <td>${detail.productSKU.skuName}</td>
                            <td>${detail.amount}</td>
                            <td>${detail.price?string('0.00')}</td>
                            <#if sale.status != "confirmed">
                                <td>
                                    <a class="btn_edit" href="${request.contextPath}/sale/order/detail?detailId=${detail.saleDetailId}&saleId=${sale.saleId}"><i class="icon icon-pencil"></i></a>
                                    <a class="btn_delete" href="#" data-key="${detail.saleDetailId}"><i class="icon icon-remove"></i></a>
                                </td>
                            </#if>
                        </tr>
                    </#list>
                    </tbody>
                    <!--<tfoot>-->
                    <!--<tr>-->
                        <!--&lt;!&ndash;<td colspan="1" class="text-right">合计：</td>&ndash;&gt;-->
                        <!--<td colspan="3"></td>-->
                        <!--<td>减免：<input type="text" class="form-control span8" /></td>-->
                        <!--<td id="td_totalPrice">${sale.totalPrice?string("0.00")}</td>-->
                        <!--<#if sale.status != "confirmed">-->
                            <!--<td>&nbsp;</td>-->
                        <!--</#if>-->
                    <!--</tr>-->
                    <!--</tfoot>-->
                </table>
                <div class="pull-right text-right" style="margin-top:20px;">
                    <!--<p>备注：<input type="text" class="form-control span10"></p>-->
                    <#if sale.status != "confirmed">
                        <p>减免：<input type="text" id="txt_cutrate" class="form-control span8" value="0.00"/></p>
                    <#elseif sale.status == "confirmed">
                        <p>减免：${sale.cutrate?string("0.00")}</p>
                    </#if>

                    <h3 >总金额：${sale.totalPrice?string("0.00")}</h3>
                    <h3>支付金额：<span id="txt_price"></span></h3>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="${request.contextPath}/js/jquery-1.12.2.min.js"></script>
<script src="${request.contextPath}/js/jquery.validate.js"></script>
<script src="${request.contextPath}/js/sweetalert.min.js"></script>
<script src="${request.contextPath}/js/select2.full.min.js"></script>
<script type="text/javascript">
    var memberId = "";
    var totalPrice = parseFloat("${sale.totalPrice}");
    var cutrate = parseFloat("${sale.cutrate}");
    $(function(){

        $("#txt_price").text((totalPrice - cutrate).toFixed(2));

        $("select").select2();

        $(".btn_edit").click(function(){
            var url = $(this).attr("href");
            url += "&memberId=" + $("select[name='memberId']").val();
            loadPage(url);
            return false;
        });

        $("#btn_back").click(function(){
            loadPage($(this).attr("href"));
            return false;
        });

        $("#btn_addProduct").click(function(){
            memberId = $("select[name='memberId']").val();
            console.log(memberId);
            $("#sale_form").submit();
        });

        $("#btn_confirm").click(function(){
            $.ajax({
                url: "${request.contextPath}/sale/order/confirm",
                type: "post",
                data: {
                    saleId: "${sale.saleId}",
                    cutrate: $("#txt_cutrate").val(),
                    payPrice: $("#txt_price").text
                },
                success: function(result){
                    swal({
                        title: "信息提示",
                        text: result.status == "success" ? "恭喜，操作已完成！" : "很遗憾，操作失败了！",
                        type: result.status,
                        animation: "slide-from-top"
                    }, function(){
                        if (result.status == "success"){
                            loadPage("${request.contextPath}/sale/order/list");
                        }
                    });
                }
            });
        });

        $("#sale_form").validate({
            submitHandler: function(form){
                $.ajax({
                    url: $(form).attr("action"),
                    type: "post",
                    data: $(form).serialize(),
                    success: function(result){
                        if (result.status == "success"){
                            if (memberId.length > 0){
                                loadPage("${request.contextPath}/sale/order/detail?saleId=" + result.data + "&memberId=" + memberId);
                            }
                        }
                    }
                });
            },
            errorClass: "help-inline",
            errorElement: "span",
            highlight:function(element, errorClass, validClass) {
                $(element).parents('.control-group').addClass('error');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).parents('.control-group').removeClass('error');
                $(element).parents('.control-group').addClass('success');
            }

        });

        $("td > .btn_delete").click(function(){
            var itemId = $(this).attr("data-key");
            swal({
                title: "信息提示",
                text: "真的要删了它？",
                type: "warning",
                showCancelButton: true,
                confirmButtonText: "是的",
                cancelButtonText: "算了",
                closeOnConfirm: false
            },
            function(isConfirm){
                if(isConfirm){
                    deleteItem(itemId);
                }
            });
            return false;
        });

        $("#txt_cutrate").keyup(function(){
            $("#txt_price").text((totalPrice - $(this).val()).toFixed(2));
        });
    });

    function deleteItem(itemId){
        $.ajax({
            url: "${request.contextPath}/sale/order/deleteDetail",
            type: "post",
            data: {
                detailId: itemId,
                saleId: "${sale.saleId}"
            },
            success: function(result){
                swal({
                    title: "信息提示",
                    text: result.status == "success" ? "恭喜，操作已完成！" : "很遗憾，操作失败了！",
                    type: result.status,
                    animation: "slide-from-top",
                    closeOnConfirm: true
                }, function(){
                    if (result.status == "success"){
                        loadPage("${request.contextPath}/sale/order/info?saleId=${sale.saleId}");
                    }
                });
            }
        });
    }
</script>